﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.Data.Core;
using Domain.MainModule.Orders;
using Infraestructure.Data.MainModule.UnitOfWork;
using Domain.Core.Specification;
using System.Globalization;
using Infraestructure.Data.MainModule.Resources;
using Infraestructure.CrossCutting.Logging;

namespace Infraestructure.Data.MainModule.Repositories
{
    public class OrderRepository : Repository<Order>, IOrderRepository
    {
        #region constructor

        public OrderRepository(IMainModuleUnitOfWork unitOfWork, ITraceManager traceManager) : base(unitOfWork, traceManager) { }

        #endregion

        #region IOrderRepository implementations

        public Order FindOrder(ISpecification<Order> specification)
        {
            //validate specification
            if (specification == (ISpecification<Phone>)null)
            {
                throw new ArgumentNullException("specification");
            }

            IMainModuleUnitOfWork context = this.UnitOfWork as IMainModuleUnitOfWork;

            if (context != null)
            {
                //perform operation in this repository
                return context.Orders.Where(specification.SatisfiedBy()).SingleOrDefault();
            }
            else
            {
                throw new InvalidOperationException(string.Format(
                                                                CultureInfo.InvariantCulture,
                                                                Messages.exception_InvalidStoreContext,
                                                                this.GetType().Name));
            }
        }

        #endregion
    }
}
